package value

import (
	"context"
	"fmt"

	"tongquetai_server/activity_vk/domain/manage/common/static_data"
)
// @META.table:json
/*
[
    ["超值礼包"],
    ["购买项ID", "礼包价格", "限购数量", "是否重置", "奖励内容", "奖励内容"],
    ["bag_wanfa_ldjz1_99", 0.99, 1, "是", "鼓舞旗子1级礼包*1", "鼓舞旗子1级礼包*1"],
    ["bag_wanfa_ldjz2_99", 0.99, 1, "否", "鼓舞旗子1级礼包*1", "鼓舞旗子1级礼包*1"]
]
*/
// @META.struct:tqt
type TqtGiftPackage struct {
	LogicalProductId string     `json:"logicalProductId" header:"购买项ID" validate:"required"`
	Price            float64    `json:"price" header:"礼包价格"`
	BuyCountLimit    int        `json:"buyCountLimit" header:"限购数量" validate:"gt=0"`
	IsDailyReset     bool       `json:"isDailyReset" header:"是否重置" true:"是" false:"否"`
	Award            []TqtAward `json:"award" header:"奖励内容" validate:"gte=0,dive"`
}

func (t *TqtGiftPackage) Call(ctx context.Context) (err error) {
	productIdMap, err := static_data.GetPayLogicalProduct().GetProductIdMap(ctx)
	if err != nil {
		return
	}
	payLogicalProduct, ok := productIdMap[t.LogicalProductId]
	if !ok {
		return fmt.Errorf("购买项ID[%s]不存在", t.LogicalProductId)
	}

	if payLogicalProduct.Price != t.Price {
		return fmt.Errorf("礼包价格错误, 填写值[%v], 实际值[%v]", t.Price, payLogicalProduct.Price)
	}

	return
}
// @META.struct:svr
type SvrGiftPackage struct {
	No    int        `json:"no"`
	Award []SvrAward `json:"award"`
}

/*
铜雀台 转 服务端
*/

func TqtToSvrGiftPackage(giftPackage TqtGiftPackage, packageMap map[string]static_data.PackageItem) (SvrGiftPackage, error) {
	packageItem, ok := packageMap[giftPackage.LogicalProductId]
	if !ok {
		return SvrGiftPackage{}, fmt.Errorf("未找到购买项ID[%s]对应的packageItem数据", giftPackage.LogicalProductId)
	}
	return SvrGiftPackage{No: packageItem.ItemNo, Award: TqtToSvrAwards(giftPackage.Award)}, nil
}

func TqtToSvrGiftPackageList(giftPackages []TqtGiftPackage, packageMap map[string]static_data.PackageItem) ([]SvrGiftPackage, error) {
	res := make([]SvrGiftPackage, len(giftPackages))
	for i, v := range giftPackages {
		svrGiftPackage, err := TqtToSvrGiftPackage(v, packageMap)
		if err != nil {
			return nil, err
		}
		res[i] = svrGiftPackage
	}

	return res, nil
}

// TqtGiftPackageToSvrActivityStore 超值礼包 转 活动商店配置
func TqtGiftPackageToSvrActivityStore(giftPackage TqtGiftPackage, packageMap map[string]static_data.PackageItem, no int) (SvrActivityStore, error) {
	packageItem, ok := packageMap[giftPackage.LogicalProductId]
	if !ok {
		return SvrActivityStore{}, fmt.Errorf("未找到购买项ID[%s]对应的packageItem数据", giftPackage.LogicalProductId)
	}
	return SvrActivityStore{
		No:            no,
		BuyCountLimit: giftPackage.BuyCountLimit,
		IsDailyReset:  giftPackage.IsDailyReset,
		Award: SvrAward{SvrItem: SvrItem{
			GameObjectId: packageItem.GameObjectId,
		}},
	}, nil
}

func TqtGiftPackageToSvrActivityStoreList(giftPackages []TqtGiftPackage, packageMap map[string]static_data.PackageItem, no int) ([]SvrActivityStore, error) {
	res := make([]SvrActivityStore, len(giftPackages))
	for i, v := range giftPackages {
		svrActivityStore, err := TqtGiftPackageToSvrActivityStore(v, packageMap, no)
		if err != nil {
			return nil, err
		}

		res[i] = svrActivityStore
		no += 1
	}

	return res, nil
}

// SvrGiftPackageActivityStoreToTqtGiftPackage 超值礼包和活动商店配置 转 超值礼包
func SvrGiftPackageActivityStoreToTqtGiftPackage(ctx context.Context, giftPackage SvrGiftPackage, activityStore SvrActivityStore, packageMap map[int]static_data.PackageItem) TqtGiftPackage {
	var (
		logicalProductId string
		price            float64
	)
	if packageItem, ok := packageMap[giftPackage.No]; ok {
		logicalProductId = packageItem.LogicalProductId
	}

	productIdMap, err := static_data.GetPayLogicalProduct().GetProductIdMap(ctx)
	if err != nil {
		productIdMap = make(map[string]static_data.PayLogicalProduct)
	}
	if payLogicalProduct, ok := productIdMap[logicalProductId]; ok {
		price = payLogicalProduct.Price
	}

	return TqtGiftPackage{
		LogicalProductId: logicalProductId,
		Price:            price,
		BuyCountLimit:    activityStore.BuyCountLimit,
		IsDailyReset:     activityStore.IsDailyReset,
		Award:            SvrToTqtAwards(ctx, giftPackage.Award),
	}
}

func SvrGiftPackageActivityStoreToTqtGiftPackageList(ctx context.Context, giftPackages []SvrGiftPackage, activityStores []SvrActivityStore, packageMap map[int]static_data.PackageItem) []TqtGiftPackage {
	res := make([]TqtGiftPackage, len(giftPackages))
	for i, v := range giftPackages {
		res[i] = SvrGiftPackageActivityStoreToTqtGiftPackage(ctx, v, activityStores[i], packageMap)
	}

	return res
}
